﻿using Engine;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using XmlUtilities;

namespace CommandBlock
{
    /// <summary>
    /// 配置资源管理
    /// </summary>
    public class CommandConfManager
    {
        public static bool Initialize()
        {
            string commandPath = DataHandle.GetCommandPath();
            string confPath = Storage.CombinePaths(commandPath, "Settings.xml");
            if (Storage.FileExists(confPath))
            {
                Stream stream = Storage.OpenFile(confPath, OpenFileMode.Read);
                try
                {
                    XElement xSettings = XmlUtils.LoadXmlFromStream(stream, Encoding.UTF8, true);
                    stream.Dispose();
                    foreach (XElement x in xSettings.Elements("Settings").ToList())
                    {
                        switch (x.Attribute("Name").Value)
                        {
                            case "WithdrawMode": WithdrawBlockManager.WithdrawMode = x.Value.Contains("True"); break;
                            case "WithdrawSteps": WithdrawBlockManager.MaxSteps = int.Parse(x.Value); break;
                            case "ShowRay": SubsystemCmdRodBlockBehavior.ShowRay = x.Value.Contains("True"); break;
                        }
                    }
                }
                catch
                {
                    stream.Dispose();
                    Log.Warning("Load Settings Fail");
                }
            }
            else
            {
                SaveWhenDispose();
                foreach (ContentInfo info in ContentManager.List())
                {
                    if (info.AbsolutePath.StartsWith("Command/"))
                    {
                        string commandFile = Storage.GetSystemPath(Storage.CombinePaths(commandPath, info.Filename));
                        FileStream fileStream = new FileStream(commandFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                        byte[] buff = new byte[info.ContentStream.Length];
                        info.ContentStream.Read(buff, 0, buff.Length);
                        fileStream.Write(buff, 0, buff.Length);
                        fileStream.Flush();
                        fileStream.Dispose();
                    }
                }
                return false;
            }
            return true;
        }

        public static void SaveWhenDispose()
        {
            string commandPath = DataHandle.GetCommandPath();
            string confPath = Storage.CombinePaths(commandPath, "Settings.xml");
            Stream stream = Storage.OpenFile(confPath, OpenFileMode.Create);
            try
            {
                XElement xSettings = new XElement("CommandConf");
                AddSaveAttribute(xSettings, "WithdrawMode", WithdrawBlockManager.WithdrawMode.ToString());
                AddSaveAttribute(xSettings, "WithdrawSteps", WithdrawBlockManager.MaxSteps.ToString());
                AddSaveAttribute(xSettings, "ShowRay", SubsystemCmdRodBlockBehavior.ShowRay.ToString());
                XmlUtils.SaveXmlToStream(xSettings, stream, Encoding.UTF8, true);
                stream.Dispose();
            }
            catch
            {
                stream.Dispose();
                Log.Warning("Save Settings Fail");
            }
        }

        public static void AddSaveAttribute(XElement xSettings, string name, string value)
        {
            XElement xtopic = new XElement("Settings");
            XmlUtils.SetAttributeValue(xtopic, "Name", name);
            xtopic.Value = value;
            xSettings.Add(xtopic);
        }
    }
}